//立项申请返现
web.loadMyk=(function(){
    return function(id){
        var id =getQueryString('id'),
            Preservation =getQueryString('Preservation');
        var Preser=Preservation==undefined?'保存':'未保存';
        var user_data = {
            customerCode:id
            //01.01.2803
        }
        var succCallBack = function (data, status, response) {
            sessionStorage.setItem("oldsysntractid",data.customer.old_sys_contract_id);
            console.log(data.customer)
            console.log(data.customer.bel_contract_id)
            sessionStorage.setItem("contractgg",data.customer.bel_contract_id);
            if(data.errorCode=="0"){
                //立项申请
                var dataInter=data.customer;
                var interText = doT.template($("#interpolationtmpl").text());
                $("#interpolation").html(interText(dataInter));
                //客户基本信息
                var interText = doT.template($("#kehu_bob").text());
                $("#kehu_er").html(interText(dataInter));
                var saleManagerInfo=dataInter.saleManagerInfo,
                    belong_groupName=dataInter.belong_groupName;
                if(saleManagerInfo==null){
                    saleManagerInfo=='12'
                }else {
                    $("#groupId_txt").text(saleManagerInfo)
                }
                $("#belGroupId").text(belong_groupName)
                //签约信息
                var interText = doT.template($("#qian_bob").text());
                $("#qian_lsit").html(interText(dataInter));
                /* $.each(data.customer,function(i,item){
                 })*/
                //销售主管查询
                // var belong_groupId=data.customer.belong_groupId;
                // var ying = {
                //     saleManagerId:belong_groupId
                // }
                // 返现运营经理
                var manager_text=data.customer.operate_manager_id;
                var manager_name=data.customer.NAME;
                $('#manager').val(manager_name).text(manager_text)
                // web.doAjax(root.inter.getSaleManagerById,'post',ying, succCallying);
                 //客户归属查询
                // var belong_sale_directorId=data.customer.belong_sale_directorId;
                // var kehu = {
                //     belGroupId:belong_sale_directorId
                // }
                // web.doAjax(root.inter.getGroupById,'post',kehu, succCallkehu);
            }
        };
        //销售主管查询
        var succCallying = function (data, status, response) {
            //console.log(data)
            if(data.errorCode=="0"){
                var text=data.saleManagerInfo.user_name;
                if(data.saleManagerInfo==null){
                    $("#groupId_txt").text("暂无数据")
                    return false
                }else{
                    $("#groupId_txt").text(text)
                }
            }
        }
        //客户归属查询
        var succCallkehu = function (data, status, response) {
            console.log(data)
            if(data.errorCode=="0"){
                var text=data.groupInfo.organization_name;
                if(data.groupInfo==null){
                    $("#belGroupId").text("暂无数据")
                    return false
                }else{
                    $("#belGroupId").text(text)
                }
                return false
            }
        }
        if(Preser=="保存"){
            $("#form-member-add").validate({
                rules: {
                    username: {
                        required: true,
                        maxlength: 30,
                    },
                    inform: {
                        required: true,
                        maxlength: 80,
                    },
                    moli:{
                        required: true,
                    }
                },
                submitHandler:function(form){
                    var quzhi=JSON.parse(web.readCookie("loginData"))
                    var customerCode =getQueryString('id'),
                        customerId =getQueryString('customerId'),
                        oldSysContractID =getQueryString('oldSysContractID'),
                        syn_cus_oldcontract_id =getQueryString('syn_cus_oldcontract_id'),
                        contractEnterDate=getQueryString('contractEnterDate'),
                        contract_num=getQueryString('contract_num'),
                        xin_contract_num=getQueryString('xin_contract_num'),
                        contract_id=getQueryString('contract_id'),
                        credit=getQueryString('credit'),
                        manager=$("#manager").text(),
                        username=$("#username").val(),
                        inform=$("#inform").val(),
                        email=$("#email").val(),
                        expressPolicy=$("#expressPolicy").val(),
                        priceSystem=$("#daku").val();
                    var user_data = {
                        id:syn_cus_oldcontract_id,
                        customerCode:customerCode,
                        oldSysContractID:oldSysContractID,
                        customerId:customerId,
                        operateManagerID:manager,
                        priceSystem:priceSystem,
                        expressPolicy:expressPolicy,
                        customerEmail:email,
                        apprTheme:username,
                        purchaseGoodsIntro:inform,
                        groupBusinessLicenseAddr:$("#fileUp1").val(),
                        groupOpeningAccountAddr:$("#fileUp2").val(),
                        groupIDcardUrl1:$("#fileUp3").val(),
                        groupIDcardUrl2:"groupIDcardUrl2",
                        groupIDcardUrl3:"groupIDcardUrl3",
                        groupIDcardUrl4:"groupIDcardUrl4",
                        groupLoa:$("#fileUp4").val(),
                        applicationNoteNum:1,
                        authConsignee:$('.consignee_name').text(),
                        apprDate:$( "#date_er").text(),
                        create_name:quzhi.user.name,
                        logo:$("#fileUp5").val(),
                    }
                    var succCallBack = function (data, status, response) {
                        console.log(data)
                        if(data.errorCode=="0"){
                            layer.msg('提交成功', {icon: 6,time:1000});
                            setTimeout( function () {
                                sessionStorage.setItem("bel_contract_id",data.contractId);
                                window.location.href="oods_pool.html?id="+customerCode+"&syn_cus_oldcontract_id="+syn_cus_oldcontract_id+"&oldSysContractID="+oldSysContractID+"&contractEnterDate="+contractEnterDate+"&customerId="+customerId+"&credit="+credit+"&contract_num="+contract_num+"&xin_contract_num="+xin_contract_num+"&contractID="+data.contractId
                            },1000);
                        }else if(data.errorCode=="50") {
                            layer.msg('请填写价格体系和运费政策',{icon: 5,time:2000});
                        }else {
                            layer.msg('程序在开小差 请及时联系管理员！',{icon: 5,time:5000});
                        }
                    }
                    return web.doAjax(root.inter.commitAppr,'post',user_data, succCallBack);
                }
            })

        }else {
            $("#form-member-add").validate({
                rules: {
                    username: {
                        required: true,
                    },
                    inform: {
                        required: true,
                        maxlength: 80,
                    },
                    moli:{
                        required: true,
                    }
                },
                submitHandler:function(form) {
                    var quzhi=JSON.parse(web.readCookie("loginData"))
                    var customerCode =getQueryString('id'),
                        customerId =getQueryString('customerId'),
                        oldSysContractID =getQueryString('oldSysContractID'),
                        syn_cus_oldcontract_id =getQueryString('syn_cus_oldcontract_id'),
                        contractEnterDate=getQueryString('contractEnterDate'),
                        contract_num=getQueryString('contract_num'),
                        xin_contract_num=getQueryString('xin_contract_num'),
                        contract_id=getQueryString('contract_id'),
                        credit=getQueryString('credit'),
                        manager=$("#manager").text(),
                        username=$("#username").val(),
                        inform=$("#inform").val(),
                        email=$("#email").val(),
                        expressPolicy=$("#expressPolicy").val(),
                        priceSystem=$("#daku").val();
                    var user_data = {
                        id:syn_cus_oldcontract_id,
                        customerCode:customerCode,
                        oldSysContractID:oldSysContractID,
                        customerId:customerId,
                        operateManagerID:manager,
                        priceSystem:priceSystem,
                        expressPolicy:expressPolicy,
                        customerEmail:email,
                        apprTheme:username,
                        purchaseGoodsIntro:inform,
                        groupBusinessLicenseAddr:$("#fileUp1").val(),
                        groupOpeningAccountAddr:$("#fileUp2").val(),
                        groupIDcardUrl1:$("#fileUp3").val(),
                        groupIDcardUrl2:"groupIDcardUrl2",
                        groupIDcardUrl3:"groupIDcardUrl3",
                        groupIDcardUrl4:"groupIDcardUrl4",
                        groupLoa:$("#fileUp4").val(),
                        applicationNoteNum:1,
                        authConsignee:$('.consignee_name').text(),
                        apprDate:$( "#date_er").text(),
                        create_name:quzhi.user.name,
                        logo:$("#fileUp5").val(),
                    }
                    var succCallBack = function (data, status, response) {
                        console.log(data)
                        if(data.errorCode=="0"){
                            layer.msg('提交成功', {icon: 6,time:1000});
                            setTimeout( function () {
                                window.location.href="oods_pool.html?id="+customerCode+"&syn_cus_oldcontract_id="+syn_cus_oldcontract_id+"&oldSysContractID="+oldSysContractID+"&contractEnterDate="+contractEnterDate+'&customerId='+customerId+'&credit='+credit+'&contract_num='+contract_num+'&xin_contract_num='+xin_contract_num+'&contractID='+contract_id
                            },1000);
                        }
                    }
                    return web.doAjax(root.inter.commitAppr,'post',user_data, succCallBack);
                }
            })
        }
        web.doAjax(root.inter.getCrmCustomerByCode,'post',user_data, succCallBack);
    }
})();
//立项申请保存
function Preservation () {
    var customerCode =getQueryString('id'),
        customerId =getQueryString('customerId'),
        oldSysContractID =getQueryString('oldSysContractID'),
        syn_cus_oldcontract_id =getQueryString('syn_cus_oldcontract_id'),
        manager=$("#manager").text(),
        username=$("#username").val(),
        inform=$("#inform").val(),
        email=$("#email").val(),
        expressPolicy=$("#expressPolicy").val(),
        priceSystem=$("#daku").val();
    if(manager=="null"){
        layer.msg('没有该运营经理', {icon: 5,time:1000});
        return false
    }
    var user_data = {
        id:syn_cus_oldcontract_id,
        customerCode:customerCode,
        oldSysContractID:oldSysContractID,
        customerId:customerId,
        operateManagerID:manager,
        priceSystem:priceSystem,
        expressPolicy:expressPolicy,
        customerEmail:email,
        apprTheme:username,
        purchaseGoodsIntro:inform,
        groupBusinessLicenseAddr:$("#fileUp1").val(),
        groupOpeningAccountAddr:$("#fileUp2").val(),
        groupIDcardUrl1:$("#fileUp3").val(),
        groupIDcardUrl2:"groupIDcardUrl2",
        groupIDcardUrl3:"groupIDcardUrl3",
        groupIDcardUrl4:"groupIDcardUrl4",
        groupLoa:$("#fileUp4").val(),
        applicationNoteNum:1,
        authConsignee:$('.consignee_name').text(),
        apprDate:$( "#date_er").text(),
        logo:$("#fileUp5").val(),
    }
    var succCallBack = function (data, status, response) {
        console.log(data)
        if(data.errorCode=="0"){
            layer.msg('保存成功', {icon: 6,time:1000});
        }else {
            layer.msg('保存失败请联系管理员', {icon: 5,time:1000});
        }
    }
    return web.doAjax(root.inter.saveAppr,'post',user_data, succCallBack);
}
//立项申请取消
function cancel() {
    window.location.href="contract.html"
}

//商品折扣率获取
function discountdata(){
 console.log('123')
 var user_data = {
        categoryId:$('#pin_list').val()
    }
    //折扣率下拉初始化
      var succCallBacks = function (data, status, response) {
        //console.log(data)
        if (data.errorCode=="0"){
            var tab_data=data.data;
            // var interText = doT.template($("#discint_bob").text());
            // $("#discint").html(interText(tab_data));
            // $(".duandata").html(data.data[0].discount_value)
            if(tab_data==''){
                cer+='<option value="'+0+'">暂无数据</option>'
                $("#discint").html(cer);
            }else{
                var cer=""
                for(var i=0; i<tab_data.length; i++){
                    cer+='<option value="'+tab_data[i].discount_value+'">'+tab_data[i].discount_value+'</option>'
                }
                $("#discint").html(cer+'<option value="0">其他</option>');
           }
        }
    };
    web.doAjax(root.inter.getDiscountByCategoryId,'post',user_data, succCallBacks);
}
//授权收货人
web.takeover=(function(){
    return function(page,current,obj,scrollLoad){
        var user_data = {
            oldContractId:"10000519",
        }
        var succCallBack = function (data, status, response) {
            console.log(data)
            if(data.errorCode=="0"){
                var tab_data=data.consignee;
                var interText = doT.template($("#tab_lsit_bob").text());
                $("#tab_lsit").html(interText(tab_data));
            }
        };
         web.doAjax(root.inter.getConsigneeByOldContractId,'post',user_data, succCallBack);
    }
})();
//品类下拉框初始化
web.takeover_add=(function(){
    //商品折扣率添加
    $('#tan_boot').bind('click',function() {
        var pin_list = $('#pin_list').val(),
            discint = $('#discint').val(),
            discount = $('#discount').val(),
            discounts = $('#discount').text();
        if(discounts==1){
            if(discount<88.00 || discount>299.99){
                layer.msg('请输入88.00~299.99之间的折扣率', {icon: 5,time:1000});
                return false
            }
        }
        var user_data = {
            applicationNoteNum:"00000000000002",
            customerId:getQueryString('customerId'),
            customerCode:getQueryString('id'),
            categoryId:pin_list,
            categoryDiscount:discint,
            expectedCategoryDiscount:discount,
        }
        var succCallBack = function (data, status, response) {
            console.log(data)
            if(data.errorCode=="0"){
                $.Huimodalalert("添加成功", 2000)
                Reappearance()
                //触发父及文件函数
                window.parent.Reappearance();
            }else{
                $.Huimodalalert(data.errorMessage, 2000)
            }
        };
         web.doAjax(root.inter.addCategoryDiscount,'post',user_data, succCallBack);
    })
    return function(page,current,obj,scrollLoad){
        var user_data = {
             categoryId:$('#pin_list').val()
            //categoryId:getQueryString('customerId')
        }
        //品类下拉初始化
        var succCallBack = function (data, status, response) {
            console.log(data.data)
            if (data.errorCode=="0"){
                var tab_data=data.data;
                console.log(tab_data)
                var interText = doT.template($("#pin_bob").text());
                $("#pin_list").html(interText(tab_data));
                // web.conlsiedata();
                // discountdata()
                // setTimeout(function(){

                // },200)
            }
        };
        web.doAjax(root.inter.getCategory,'post',user_data, succCallBack);
        //折扣率下拉初始化
        var succCallBacks = function (data, status, response) {
            //console.log(data)
            if (data.errorCode=="0"){
                var tab_data=data.data;
                var interText = doT.template($("#discint_bob").text());
                $("#discint").html(interText(tab_data));
            }
        };
        web.doAjax(root.inter.getDiscountByCategoryId,'post',user_data, succCallBacks);
        //通过折扣率查询列表返回每一个id获取名称
        var succCallBack_id = function (data, status, response) {
            console.log(data)
            if (data.errorCode=="0"){

            }
        }
        //通过折扣率查询列表返回每一个id获取名称
        web.doAjax(root.inter.getCategoryDetailByCategoryId,'post',user_data, succCallBack_id);
    }
})();
//品类折扣率删除
function shang_delete(id) {
    console.log(id)
    var user_data = {
        id:id,
        customerId:getQueryString('customerId')
    }
    layer.confirm("点击确定删除本条数据",function(index){
        var succCallBack = function (data, status, response) {
            console.log(data)
            if(data.errorCode=="0") {
                if(!Reappearance()){
                    layer.msg('删除成功', {icon: 6,time:1000});
                }
                //触发父及文件函数
                window.parent.Reappearance();
            }
        };
        return web.doAjax(root.inter.deleteCategoryDiscount, 'post', user_data, succCallBack);
    });
}
//去入住品类折扣率返现
function Reappearance() {
    //通过商品折扣率id查询列表
    var user_data_shang = {
        customerId:getQueryString('customerId')
    }
    //通过商品折扣率id查询列表
    var succCallBack_list = function (data, status, response) {
        console.log(data)
        if (data.errorCode=="0"){
            var tab_data=data.data;
            var datance=''
            for ( var i=0; i<tab_data.length; i++){
                var category_discount=tab_data[i].category_discount
                     if(category_discount==0){
                         category_discount=''
                     }
                var expected_category_discount=tab_data[i].expected_category_discount
                if(expected_category_discount==-1){
                    expected_category_discount=''
                }
                datance+='<tr>'
                       +'<td >'+tab_data[i].categoryName+'</td>'
                       +'<td >'+category_discount+'%</td>'
                       +'<td >'+expected_category_discount+'</td>'
                       +'<td ><a onclick="shang_delete('+tab_data[i].category_id+')"><i class="Hui-iconfont f-18">&#xe609;</i></a></td>'
                       +'</tr>'
            }
            console.log(datance)
            $("#bootbrsli").html(datance);
        }else {
            $.Huimodalalert(data.errorMessage, 2000)
        }
    }
    web.doAjax(root.inter.getAllCategoryDiscountAPI,'post',user_data_shang, succCallBack_list);
}


















